package com.lrd.rmq514version.rmq.service;

import com.lrd.rmq514version.rmq.producer.MyDelayTimeMsgProducer;
import com.lrd.rmq514version.rmq.vo.SendMoreDelayTimeMsgVo;
import com.lrd.rmq514version.rmq.vo.SendOneDelayTimeMsgVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

@Service
public class RmqService {

    private static final Logger logger = LoggerFactory.getLogger(RmqService.class);

    @Autowired
    private MyDelayTimeMsgProducer myDelayTimeMsgProducer;

    /**
     * 发送一条
     *
     * @param sendOneDelayTimeMsgVo
     * @return
     */
    public String sendDelayTimeMessage(SendOneDelayTimeMsgVo sendOneDelayTimeMsgVo) {
        if (sendOneDelayTimeMsgVo == null
                || sendOneDelayTimeMsgVo.getDelayTimeMs() == null
                || StringUtils.isBlank(sendOneDelayTimeMsgVo.getKey())
                || StringUtils.isBlank(sendOneDelayTimeMsgVo.getMessage())) {
            throw new RuntimeException("参数不能为空[delayTimeMs,key,message]");
        }
        String message = sendOneDelayTimeMsgVo.getMessage();
        String key = sendOneDelayTimeMsgVo.getKey();
        Long delayTimeMs = sendOneDelayTimeMsgVo.getDelayTimeMs();
        boolean sendResult = myDelayTimeMsgProducer.sendDelayTimeMessage(message, key, delayTimeMs);
        logger.info("调用接口发送延时消息sendResult={}", sendResult);
        return "success";
    }

    /**
     * 使用循环批量发送
     *
     * @param sendMoreDelayTimeMsgVo
     * @return
     */
    public String sendMoreDelayTimeMessage(SendMoreDelayTimeMsgVo sendMoreDelayTimeMsgVo) {
        if (sendMoreDelayTimeMsgVo == null
                || sendMoreDelayTimeMsgVo.getSize() == null
                || sendMoreDelayTimeMsgVo.getSize() <= 0
                || StringUtils.isBlank(sendMoreDelayTimeMsgVo.getMessage())) {
            throw new RuntimeException("参数不能为空[size>0,message]");
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String key = LocalDateTime.now().format(formatter);
        int size = sendMoreDelayTimeMsgVo.getSize();
        //发送延时消息delayTime设置：1秒～30天
        // 1s,2s,5s,10,20,30s,60s,
        // 2m,5m,10m,20m,30m,
        // 1h,2h,3h,4h,5h,6h,7h,8h,9h,10h,12h,
        // 1d,2d,3d,4d,5d...30d
        long[] delayTimeMsArr = {
                1000, 2000, 5000, 10000, 20000, 30000, 60000,
                120000, 300000, 600000, 1200000, 1800000,
                3600000, 2 * 3600000, 3 * 3600000, 4 * 3600000, 5 * 3600000, 6 * 3600000, 7 * 3600000, 8 * 3600000, 9 * 3600000, 10 * 3600000, 11 * 3600000, 12 * 3600000,
                24 * 3600000, 2 * 24 * 3600000, 3 * 24 * 3600000, 4 * 24 * 3600000, 5 * 24 * 3600000, 6 * 24 * 3600000, 7 * 24 * 3600000, 8 * 24 * 3600000, 9 * 24 * 3600000, 10 * 24 * 3600000,
                11 * 24 * 3600000, 12 * 24 * 3600000, 13 * 24 * 3600000, 14 * 24 * 3600000, 15 * 24 * 3600000, 16 * 24 * 3600000, 17 * 24 * 3600000, 18 * 24 * 3600000, 19 * 24 * 3600000, 20 * 24 * 3600000,
                21 * 24 * 3600000, 22 * 24 * 3600000, 23 * 24 * 3600000, 24 * 24 * 3600000, 25L * 24 * 3600000, 26L * 24 * 3600000, 27L * 24 * 3600000, 28L * 24 * 3600000, 29L * 24 * 3600000, 30L * 24 * 3600000};
        //
        Map<Long, String> delayTimeMsMap = delayTimeMsMap(delayTimeMsArr);

        int restart = 0;
        for (int i = 0; i < size; i++) {
            if (delayTimeMsArr.length == restart) {
                restart = 0;
            }
            Long delayTimeMs = delayTimeMsArr[restart];
            String message = sendMoreDelayTimeMsgVo.getMessage() + "for循环index：" + i + "，延时时间：" + delayTimeMs + "(ms)，【" + delayTimeMsMap.get(delayTimeMs) + "】";
            boolean sendResult = myDelayTimeMsgProducer.sendDelayTimeMessage(message, key, delayTimeMs);
            logger.info("调用接口发送延时消息sendResult={}", sendResult);
            //
            restart++;
        }
        return "success";
    }

    /**
     * 延时毫秒<对应的>中文描述
     *
     * @param delayTimeMsArr
     * @return
     */
    private Map<Long, String> delayTimeMsMap(long[] delayTimeMsArr) {
        Map<Long, String> delayTimeMap = new HashMap<>();
        for (long delayTimeMs : delayTimeMsArr) {
            delayTimeMap.put(delayTimeMs, delayTimeStr(delayTimeMs));
        }
        return delayTimeMap;
    }

    /**
     * 延时毫秒转中文描述
     *
     * @param delayTimeMs
     * @return
     */
    private String delayTimeStr(long delayTimeMs) {
        String delayTimeStr = "";
        if (delayTimeMs <= 60000) {
            delayTimeStr = (delayTimeMs / 1000) + "秒";
        } else if (delayTimeMs <= 1800000) {
            delayTimeStr = (delayTimeMs / 60000) + "分钟";
        } else if (delayTimeMs <= 12 * 3600000) {
            delayTimeStr = (delayTimeMs / 3600000) + "小时";
        } else if (delayTimeMs <= 30L * 24 * 3600000) {
            delayTimeStr = (delayTimeMs / (24 * 3600000)) + "天";
        } else {
            delayTimeStr = null;
        }
        return delayTimeStr;
    }

}
